package com.fsnbigdata.repository;
import com.fsnbigdata.domain.Park;
import com.fsnbigdata.domain.QPark;
import com.querydsl.core.types.Predicate;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt;
import org.springframework.roo.addon.layers.repository.jpa.annotations.RooJpaRepositoryCustomImpl;
import com.fsnbigdata.domain.PlantConservation;
import com.fsnbigdata.domain.QPlantConservation;
import com.querydsl.core.types.Path;
import com.querydsl.jpa.JPQLQuery;
import io.springlets.data.domain.GlobalSearch;
import io.springlets.data.jpa.repository.support.QueryDslRepositorySupportExt.AttributeMappingBuilder;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;

/**
 * = PlantConservationRepositoryImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJpaRepositoryCustomImpl(repository = PlantConservationRepositoryCustom.class)
@Transactional(readOnly = true)
public class PlantConservationRepositoryImpl extends QueryDslRepositorySupportExt<PlantConservation> implements PlantConservationRepositoryCustom {

    /**
     * TODO Auto-generated constructor documentation
     */
    PlantConservationRepositoryImpl() {
        super(PlantConservation.class);
    }

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ORGANISM_ID = "organismId";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String CREATOR = "creator";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String GENUS = "genus";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String SPECIES = "species";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String LATIN = "latin";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String NAME = "name";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String FAMILY = "family";

    @Override
    public Page<PlantConservation> findAll(Predicate predicate, Pageable pageable) {
        QPlantConservation plantConservation = QPlantConservation.plantConservation;
        JPQLQuery<PlantConservation> query = from(plantConservation).where(predicate)
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchAll();
        return loadPage(query, pageable, plantConservation);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<PlantConservation> findAll(GlobalSearch globalSearch, Pageable pageable, String creator) {
        QPlantConservation plantConservation = QPlantConservation.plantConservation;
        JPQLQuery<PlantConservation> query = from(plantConservation);
        if(creator != null ){
            query.where(plantConservation.creator.eq(creator));
        }
        Path<?>[] paths = new Path<?>[] { plantConservation.name, plantConservation.latin, plantConservation.organismId, plantConservation.family, plantConservation.genus, plantConservation.species, plantConservation.creator };
        applyGlobalSearch(globalSearch, query, paths);
        AttributeMappingBuilder mapping = buildMapper().map(NAME, plantConservation.name).map(LATIN, plantConservation.latin).map(ORGANISM_ID, plantConservation.organismId).map(FAMILY, plantConservation.family).map(GENUS, plantConservation.genus).map(SPECIES, plantConservation.species).map(CREATOR, plantConservation.creator);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, plantConservation);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ids
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<PlantConservation> findAllByIdsIn(List<Long> ids, GlobalSearch globalSearch, Pageable pageable) {
        QPlantConservation plantConservation = QPlantConservation.plantConservation;
        JPQLQuery<PlantConservation> query = from(plantConservation);
        Path<?>[] paths = new Path<?>[] { plantConservation.name, plantConservation.latin, plantConservation.organismId, plantConservation.family, plantConservation.genus, plantConservation.species, plantConservation.creator };
        applyGlobalSearch(globalSearch, query, paths);
        // Also, filter by the provided ids
        query.where(plantConservation.id.in(ids));
        AttributeMappingBuilder mapping = buildMapper().map(NAME, plantConservation.name).map(LATIN, plantConservation.latin).map(ORGANISM_ID, plantConservation.organismId).map(FAMILY, plantConservation.family).map(GENUS, plantConservation.genus).map(SPECIES, plantConservation.species).map(CREATOR, plantConservation.creator);
        applyPagination(pageable, query, mapping);
        applyOrderById(query);
        return loadPage(query, pageable, plantConservation);
    }
}
